import { CoolController, BaseController } from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { ProviderDepositEntity } from '../../entity/deposit';
import { ProviderDepositService } from '../../service/deposit';
import { DeductDepositDTO } from '../../dto/deposit';
import { ProviderProfileEntity } from '../../entity/profile';

/**
 * 管理后台 - 服务人员质保金管理
 */
@CoolController({
  api: ['page', 'info'],
  entity: ProviderDepositEntity,
  service: ProviderDepositService,
  pageQueryOp: {
    keyWordLikeFields: ['a.orderNo', 'a.remark'],
    fieldEq: ['a.providerId', 'a.operationType', 'a.status'],
    select: ['a.*', 'b.name as providerName', 'b.phone as providerPhone'],
    join: [
      {
        entity: ProviderProfileEntity,
        alias: 'b',
        condition: 'a.providerId = b.providerId',
        type: 'leftJoin',
      },
    ],
    addOrderBy: {
      id: 'DESC',
    },
  },
})
export class AdminProviderDepositController extends BaseController {
  @Inject()
  ctx: Context;

  @Inject()
  depositService: ProviderDepositService;

  /**
   * 扣除质保金
   */
  @Post('/deduct', { summary: '扣除质保金' })
  async deduct(@Body() dto: DeductDepositDTO) {
    const result = await this.depositService.deduct(dto, this.ctx.admin?.userId);
    return this.ok(result);
  }

  /**
   * 获取服务人员质保金统计
   */
  @Post('/statistics', { summary: '获取服务人员质保金统计' })
  async statistics(@Body('providerId') providerId: number) {
    const data = await this.depositService.getStatistics(providerId);
    return this.ok(data);
  }
}

